package com.jstyle.jclifexd.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.adapter.DetailsAdapter;
import com.jstyle.jclifexd.model.Createddata;
import com.jstyle.jclifexd.model.JoinUserResult;
import com.jstyle.jclifexd.utils.StatusBarUtil;
import com.jstyle.jclifexd.utils.Utils;
import com.jstyle.jclifexd.view.GridRecyclerView;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 活动详情人数
 */
public class DetailscountActivity extends BaseActivity {
    @BindView(R.id.count_RecyclerView)
    GridRecyclerView count_RecyclerView;
    @BindView(R.id.title)
    Button title;

    DetailsAdapter detailsAdapter;
    Createddata createddata;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailscount);
        StatusBarUtil.setRootViewFitsSystemWindows(this,true);
        StatusBarUtil.setTranslucentStatus(this);
        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
            StatusBarUtil.setStatusBarColor(this,0x55000000);
        }
        ButterKnife.bind(this);
        if (null != getIntent().getSerializableExtra("Createddata")) {
            createddata = (Createddata) getIntent().getSerializableExtra("Createddata");
            Querydata();
        }
        count_RecyclerView.setLayoutManager(new GridLayoutManager(this,5));
        detailsAdapter=new DetailsAdapter(this);
        count_RecyclerView.setAdapter(detailsAdapter);

    }

    private void Querydata() {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", NetWorkUtil.getUserId());//"uid" -> "111000036"
        map.put("actId", createddata.getActId()+"");
        NetWorkUtil.getInstance().getJstyleApi().queryJoinUser(map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<JoinUserResult>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(JoinUserResult activitvtiesData) {
                if(null!=activitvtiesData&&null!=activitvtiesData.getData()&&activitvtiesData.getData().size()>0){
                    title.setText(getResources().getString(R.string.active_members)+"("+activitvtiesData.getData().size()+")");
                    detailsAdapter.SetData(activitvtiesData.getData());
                    LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(count_RecyclerView.getContext(),
                            R.anim.grid_layout_animation_from_bottom);
                    count_RecyclerView.setLayoutAnimation(controller);
                    Objects.requireNonNull(count_RecyclerView.getAdapter()).notifyDataSetChanged();
                    count_RecyclerView.scheduleLayoutAnimation();
                }

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });

    }



    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        if (Utils.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }


}
